
#include <BLabel>
#include <BImage>
#include <BIcon>
#include <BVBoxLayout>
#include <BGridLayout>
#include <BHBoxLayout>
#include <BFileDialog>
#include <BColorDialog>
#include <BStyle>

#include "../StringUtils.h"
#include "NodePanel.h"

NodePanel::NodePanel(BWidget* parent) : BGroupBox(parent)
{
	this->setTitle("Node");

	_node = 0;

	BHBoxLayout* topLayout = new BHBoxLayout();
	{
		_insertButton = new BButton();
		_insertButton->setTips("Insert a shape");
		_insertButton->setForm(Form_IconOnly);

		topLayout->addSpring();
		topLayout->addWidget(_insertButton);
	}

	BGridLayout* gridLayout = new BGridLayout();
	{
		BLabel* visibleLabel = new BLabel("Visible");
		_visibleCheckBox = new BCheckBox();

		BLabel* optimizedLabel = new BLabel("Optimized");
		_optimizedCheckBox = new BCheckBox();

		BVBoxLayout* sizeLayout = new BVBoxLayout();
		{
			_lenXSpinBox = new BRealSpinBox();
			_lenYSpinBox = new BRealSpinBox();
			_lenZSpinBox = new BRealSpinBox();
			sizeLayout->addWidget(_lenXSpinBox);
			sizeLayout->addWidget(_lenYSpinBox);
			sizeLayout->addWidget(_lenZSpinBox);
		}

		gridLayout->addWidget(0, 0, visibleLabel);
		gridLayout->addWidget(0, 1, _visibleCheckBox);
		gridLayout->addWidget(1, 0, optimizedLabel);
		gridLayout->addWidget(1, 1, _optimizedCheckBox);
		gridLayout->addWidget(2, 0, new BLabel("Size"));
		gridLayout->addLayout(2, 1, sizeLayout);
		gridLayout->setRowSpacing(3);
		gridLayout->setColumnSpacing(3);
		gridLayout->setColumnPolicy(0, Policy_Preferred);
	}

	BVBoxLayout* vlayout = new BVBoxLayout(this);
	vlayout->addLayout(topLayout);
	vlayout->addLayout(gridLayout);

	connect(this, Signal_AssetChanged, &NodePanel::slotAssetChanged);

	connect(_visibleCheckBox, Signal_Toggled, &NodePanel::slotVisibleCheckBoxToggled);
	connect(_optimizedCheckBox, Signal_Toggled, &NodePanel::slotOptimizedToggled);

}
NodePanel::~NodePanel()
{

}

void NodePanel::slotAssetChanged()
{
	_node = this->asset();
	if (_node)
	{
		_visibleCheckBox->setChecked(_node->visible());
		_optimizedCheckBox->setChecked(_node->optimized());
		const BBox& box = _node->box();
		_lenXSpinBox->setValue(box.lena());
		_lenYSpinBox->setValue(box.lenb());
		_lenZSpinBox->setValue(box.lenc());
	}
}
void NodePanel::slotVisibleCheckBoxToggled(BObject* object, const BValue& value)
{
	bool visible = value;
	_node->setVisible(visible);
	if (visible)
		emit("object-shown", _node);
	else
		emit("object-hidden", _node);
}
void NodePanel::slotOptimizedToggled(BObject* object, const BValue& value)
{
	bool optimized = value;
	_node->setOptimized(optimized);
}

void NodePanel::styleEvent(const BEvent& event)
{
	BGroupBox::styleEvent(event);
	if (const BStyle* style = this->realStyle())
	{
		_insertButton->setIcon(style->icon("insert"));
	}
}
